package com.yanqu.road.dao.impl.activity.fellingtrees;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.fellingtrees.FellingTreesRankEquipData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FellingTreesRankEquipDataDaoImpl extends TempDataObjectDao<FellingTreesRankEquipData> {

    public static String getFellingTreesRankEquipDataSql = "select * from `t_u_felling_trees_rank_equip` where `activityId`=?";

    public Map<Long, FellingTreesRankEquipData> getFellingTreesRankEquipDataMap(int activityId) {
        Map<Long, FellingTreesRankEquipData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getFellingTreesRankEquipDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FellingTreesRankEquipData parse = parse(rs);
                map.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateFellingTreesRankEquipDataSql = "update `t_u_felling_trees_rank_equip` set param=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(FellingTreesRankEquipData fellingTreesRankEquipData) {
        fellingTreesRankEquipData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, fellingTreesRankEquipData.getParam()));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesRankEquipData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesRankEquipData.getUserId()));
        if (getDb().executeNoneQuery(updateFellingTreesRankEquipDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertFellingTreesRankEquipDataSql = "insert into `t_u_felling_trees_rank_equip`(activityId,userId,param) values (?,?,?)";

    @Override
    protected boolean addData(FellingTreesRankEquipData fellingTreesRankEquipData) {
        fellingTreesRankEquipData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesRankEquipData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesRankEquipData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, fellingTreesRankEquipData.getParam()));
        if (getDb().executeNoneQuery(insertFellingTreesRankEquipDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected FellingTreesRankEquipData parseData(ResultSet rs) throws SQLException {
        FellingTreesRankEquipData fellingTreesRankEquipData = new FellingTreesRankEquipData();
        fellingTreesRankEquipData.setActivityId(rs.getInt("activityId"));
        fellingTreesRankEquipData.setUserId(rs.getLong("userId"));
        fellingTreesRankEquipData.setParam(rs.getString("param"));
        return fellingTreesRankEquipData;
    }

}
